<?php

class McCube_Html_Listing_Column{

    protected $_name          = '';
    protected $_label         = '';
    protected $_type          = 'string';
    protected $_sortIcons     = array('desc'=>'sort-up', 'asc'=>'sort-down');
    protected $_listing       = null;

    protected $_sorted        = false;
    protected $_sortAction    = false;
    protected $_filterAction  = false;
    protected $_filterValues  = array();

    /** */
    public function __construct($listing, $name, $config=array()){
        $this->_name    = $name;
        $this->_listing = $listing;
        $this->_config  = $config;

        if(isset($config['type']))
            $this->setLabel($config['type']);

        if(isset($config['label']))
            $this->setLabel($config['label']);

        if(isset($config['sort']))
            $this->canBeSorted($config['sort']);

        if(isset($config['filter']))
            $this->canBeFiltered($config['filter']);

        if(isset($config['group']))
            $this->canBeGrouped($config['group']);
    }

    /** */
    public function canBeSorted($can=true){
        if(is_string($can)){
            $this->setSort($can);
            $can = true;
        }
        $this->_sortAction = $can;
        return $this;
    }

    /** */
    public function canBeFiltered($can=true){
        if(is_array($can)){
            $this->setFilterValues($can);
            $can = true;
        }
        $this->_filterAction = $can;
        return $this;
    }

    /** */
    public function getConfig(){
        return $this->_config;
    }

    /** */
    public function getName(){
        return $this->_name;
    }

    /** */
    public function getType(){
        return $this->_type;
    }

    /** */
    public function getLabel(){
        return $this->_label;
    }

    /** */
    public function setSort($sorted='asc'){
        $this->_sortIcons[$sorted] .= '-on';
        $this->_sorted = $sorted;
        return $this;
    }

    /** */
    public function isSortable(){
        return $this->_sortAction;
    }

    /** */
    public function isFilterable(){
        return $this->_filterAction;
    }

    /** */
    public function setLabel($label){
        $this->_label = $label;
        return $this;
    }

    /** */
    public function setFilterValues($filterValues){
        $this->_filterValues = $filterValues;
        return $this;
    }

    /** */
    public function getFilterValues(){
        return $this->_filterValues;
    }

    public function render($href){
        $echo = $this->_label;
        foreach($this->_sortIcons as $type=>$img){
            $echo .= McCube_Html::getIcon()
                ->a      ($href.'&orderBy['.$this->_name.']='.$type)
                ->img    ('icons/'.$img)
                ->onClick($this->_listing->getJsAction('sort'))
                ->render ()
            ;
        }
        return $echo;
    }
}